const app = getApp();
import http from '../../../utils/http'
Page({
  data: {
    name: '',
    studentId: '',
  },
  
  /**** 接口交互 ****/
  // 信息认证
  submit() {
    if (!this.checkForm()) {
      return;
    }
    http({
      url: '/api/user/bind',
      data: {
        name: this.data.name,
        studentId: this.data.studentId,
      },
      method: 'PUT',
      success: (result) => {
        wx.showLoading({
          title: '加载中',
          mask: true
        });
        wx.login({
          success: res => {
            if (res.code) {
              wx.request({
                url: app.globalData.domainName + '/wxLogin',
                data: {
                  code: res.code
                },
                method: 'POST',
                header: {
                  'content-type': 'application/x-www-form-urlencoded'
                },
                success: function (res) {
                  wx.hideLoading()
                  if (res.data.success) {
                    app.globalData.sessionId = res.data.data.sessionId;
                    app.globalData.roleId = res.data.data.roleId;
                    wx.showModal({
                      title: '提示',
                      content: '绑定成功！点击确认返回个人页面',
                      success(res) {
                        if (res.confirm) {
                          wx.switchTab({
                            url: '../../me/me'
                          })
                        } else {
                
                        }
                      }
                    })
                  }
                },
                fail: function () {
                  wx.hideLoading()
                  wx.showModal({
                    title: '提示',
                    content: '服务器连接失败，请检查网络',
                  })
                },
              })
            } else {
              wx.hideLoading()
              wx.showModal({
                title: '提示',
                content: '服务器连接失败，请检查网络',
              })
              console.log('获取用户登录态失败！' + res.errMsg)
            }
          },
          fail: function () {
            wx.hideLoading()
            wx.showModal({
              title: '提示',
              content: '服务器连接失败，请检查网络',
            })
          },
        })  
      }
    })
  },

  /**** 表单交互 ****/
  // 表单验证
  checkForm: function() {
    if(!this.data.studentId || !this.data.name) {
      wx.showToast({
        title: '不能为空',
        icon: 'error',
        duration: 500
      });
      return false;
    }
    return true;
  },
  
  // 其他输入绑定
  bindinput(){}

})